<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>账单添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/index.js"></script>
    <style>
        .layui-form {
            padding: 20px 50px;
        }
    </style>
</head>

<body>
<form class="layui-form layui-form-pane">
    <div class="layui-form-item" lay-filter="bill-form">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input type="text" name="cardname" lay-verify="required" placeholder="请输入卡片名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">卡号</label>
        <div class="layui-input-block">
            <input type="text" name="cardnum" lay-verify="required" placeholder="请输入卡号" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">卡片类别</label>
        <div class="layui-input-block">
            <select id="cardType" name="cardType">
            </select>
        </div>
    </div>
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="add" style="display:none;">立即提交</button>
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="update" style="display:none; margin-left: 0;">
            立即提交
        </button>
        <button type="reset" id="resetForm" style="display: none;">重置</button>
    </div>
</form>
<script>
    getCardType();
    var url = location.href;
    if (url.indexOf("#2") != -1) {
        $("button[lay-filter=update]").css("display", "block");
        var index = getQueryString("index");
        initForm(parent.document.getElementsByTagName("tr")[index]);
    } else {
        if (url.indexOf("#3") != -1) {
            initForm();
        }
        $("button[lay-filter=add]").css("display", "block");
    }

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;


        //监听提交
        form.on('submit(add)', function (data) {
            console.log(data);
            var type = data.field.cardType;
            data.field.type = type;
            data.field.typename = $("#cardType").find("option:selected").text();
            $.post("/card/addCard", data.field, function (res) {
                console.log(data);
                if (res.code == 200) {
                    layer.confirm("添加成功！", {
                        title: false,
                        closeBtn: false,
                        skin: 'layui-layer-molv',
                        btn: ["继续添加", "取消"]
                    }, function (layno) {
                        layer.close(layno);
                        $("#resetForm").click();
                    }, function () {
                        layer.msg(res.msg, {time: 1000}, function () {
                            x_admin_close();
                            if (parent.location.href.indexOf("details.html") != -1) {
                                parent.getCards();
                            } else {
                                parent.document.getElementById("home").src = "welcome";
                            }
                        });
                    });
                } else {
                    layer.msg(res.msg)
                }
            })
            return false; //防止form的action提交
        });

        //监听提交
        form.on('submit(update)', function (data) {
            // console.log(data);
            layer.confirm("确认修改？", {title: false, closeBtn: false, skin: 'layui-layer-molv'}, function (layno) {
                layer.close(layno);
                var type = data.field.cardType;
                data.field.type = type;
                data.field.typename = $("#cardType").find("option:selected").text();
                $.post("/card/updateCard", data.field, function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            x_admin_close();
                            parent.getCards();
                        });
                    } else {
                        layer.msg(res.msg)
                    }
                });
            });
            return false;   //防止form的action提交
        });
    });

    function initForm(tr) {
        // console.log(tr);
        if (tr == undefined) {
            return;
        } else {
            $("input[name=cardname]").val($(tr).find(":nth-child(2)").html());
            $("input[name=cardnum]").val($(tr).find(":nth-child(3)").html());
            var cardType = $(tr).find(":nth-child(8)").html();
            console.log(cardType)
            $("#cardType").find("option[value='" + cardType + "']").attr("selected", true);
            $("input[name=id]").val($(tr).find(":nth-child(7)").html());
        }
    }
</script>
</body>

</html>
